package com.weblog.model.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "tb_role")
public class Role implements Serializable{

    private static final long serialVersionUID=-1266324678942943999L;


    /**
     * 角色id
     */
    @Id
    private String rid;

    /**
     * 角色名称
     */
    private String rname;

    /**
     * 创建时间
     */
    private Date createtime;
    /**
     * 创建者
     */
    private String createuser;
    /**
     * 最后修改时间
     */
    private Date updatetime;
    /**
     * 状态 0弃用 1使用
     */
    private String state;

    /**
     * 0自定义角色
     * 1系统角色
     */
    private String issystemrole;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles",fetch = FetchType.EAGER)
    private Set<Admin>admins=new HashSet<>();


    @JsonIgnore
    /**
     * @JsonIgnoreProperties 忽略所得permissions中的roles
     */
    @JsonIgnoreProperties(value = {"roles"})
    @ManyToMany(targetEntity = Permission.class,fetch = FetchType.EAGER)
    @JoinTable(name = "tb_rp",
            joinColumns = {@JoinColumn(name = "rid",referencedColumnName = "rid")},
            inverseJoinColumns = {@JoinColumn(name = "pid",referencedColumnName = "pid")})
    private Set<Permission>permissions=new HashSet<>();


    public Role() {
    }

    public Role(String rid, String rname, String state) {
        this.rid = rid;
        this.rname = rname;
        this.state = state;
    }


    @Override
    public String toString() {
        return "Role{" +
                "rid='" + rid + '\'' +
                ", rname='" + rname + '\'' +
                ", createtime=" + createtime +
                ", createuser='" + createuser + '\'' +
                ", updatetime=" + updatetime +
                ", state='" + state + '\'' +
                ", issystemrole='" + issystemrole + '\'' +
                ", permissions=" + permissions +
                '}';
    }

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    public String getIssystemrole() {
        return issystemrole;
    }

    public void setIssystemrole(String issystemrole) {
        this.issystemrole = issystemrole;
    }

    public Role(String rid, String rname, Date createtime, String createuser, Date updatetime, String state, Set<Admin> admins) {
        this.rid = rid;
        this.rname = rname;
        this.createtime = createtime;
        this.createuser = createuser;
        this.updatetime = updatetime;
        this.state = state;
        this.admins = admins;
    }


    public Set<Admin> getAdmins() {
        return admins;
    }

    public void setAdmins(Set<Admin> admins) {
        this.admins = admins;
    }

    public String getRid() {
        return rid;
    }

    public void setRid(String rid) {
        this.rid = rid;
    }

    public String getRname() {
        return rname;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getCreateuser() {
        return createuser;
    }

    public void setCreateuser(String createuser) {
        this.createuser = createuser;
    }

    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
